package com.dingpeikeji.quickdeer.mobile.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

public class DownLoadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取下载文件的名称
		String filename = request.getParameter("name");
		//注意中文乱码:
		filename=new String(filename.getBytes("iso8859-1"),"utf-8");
		ServletContext context = this.getServletContext();
		//文件下载
		//1.设置文件的mimeType
		String mimeType = context.getMimeType(filename);
		response.setContentType(mimeType);
		//方式2:网络上的方式 (8成好使)
		response.setHeader("content-disposition", "attachment;filename="+new String(filename.getBytes("gbk"),"iso8859-1"));
		//3.对拷流
		//获取输入流
		InputStream is = context.getResourceAsStream("/download/"+filename);
		//获取输出流
		ServletOutputStream os = response.getOutputStream();
		
		IOUtils.copy(is, os);
		os.close();
		is.close();
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
